package com.example.taobao

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class shopAdapter(val content : Context, val shopList: List<shop>) : RecyclerView.Adapter<shopAdapter.ViewHoilder>() {


    private lateinit var onItemClickListener: OnItemClickListener

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener

    }

    inner class ViewHoilder(view:View) : RecyclerView.ViewHolder(view){
        val shopImage :ImageView = view.findViewById(R.id.pictures)
        val shopInfo :TextView = view.findViewById(R.id.describe)
        val shopPrice : TextView = view.findViewById(R.id.shop_price)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoilder {
        val view = LayoutInflater.from(content).inflate(R.layout.shop_card, parent, false)
        return ViewHoilder(view)
    }

    override fun getItemCount(): Int {
        return shopList.size
    }

    override fun onBindViewHolder(holder: ViewHoilder, position: Int) {
        val shop = shopList[position]
        holder.shopInfo.text = shop.info
        holder.shopPrice.text = shop.price
        Glide.with(content).load(shop.src).into(holder.shopImage)


        holder.shopImage.setOnClickListener {
            onItemClickListener?.onItemClick(holder.itemView, position)

        }

    }

    interface OnItemClickListener{
        fun onItemClick(view: View, position: Int)
    }
}